/*
 * To change this template, choose Tools | Templates
 * and open the template in the editor.
 */

package com.yurendao.OSFileSystem.myTree;

import java.util.ArrayList;

import javax.swing.JTree;
import javax.swing.tree.DefaultMutableTreeNode;
import javax.swing.tree.DefaultTreeModel;

import com.yurendao.OSFileSystem.MyFAT.FATStructure;
import com.yurendao.OSFileSystem.MyFAT.MyFAT;
import com.yurendao.OSFileSystem.file.MyFile;
import com.yurendao.OSFileSystem.folder.MyFolder;


/**
 *
 * @author Administrator
 */
public class MyFileSystemTree {

    private JTree mytree;

    public MyFileSystemTree(MyFAT myfat,JTree temp) {
        DefaultMutableTreeNode mycomputer = new DefaultMutableTreeNode("MyComputer");
        this.searchMyTree(myfat,mycomputer);
        temp .setModel(new DefaultTreeModel(mycomputer));
        this.mytree = temp;
  
    }

    public void setMyTree(JTree temp){
        mytree = temp;
    }

    public JTree getMyTree(){
        return this.mytree;
    }

    //用于 显示整个文件分配表的 树形结构
    private void searchMyTree(MyFAT myfat,DefaultMutableTreeNode mycomputer){
        this.searchNode(myfat, 0, mycomputer);            
    }
    //查找 某个节点 所对应的子树
    private void searchNode(MyFAT myfat,int parentFATNumber,DefaultMutableTreeNode parent){
        if(myfat.getAllfilesOfOneFolder(parentFATNumber).size()<=0){
            
        }else{
            ArrayList childrenNodes = myfat.getAllfilesOfOneFolder(parentFATNumber);
            for(int i=0;i<childrenNodes.size();i++){
                DefaultMutableTreeNode me;
                FATStructure temp = (FATStructure)childrenNodes.get(i);
                if(myfat.isaFolder(temp.getFatType())){
                    me = new DefaultMutableTreeNode(((MyFolder)temp.getFatContent()).getMyfolderAttributes().getFolderName());
//                    me.setUserObject(temp);
//                    me = new DefaultMutableTreeNode(temp);
                    
                    searchNode(myfat,((MyFolder)temp.getFatContent()).getMyFATNumber(),me);
                }else{
                    me = new DefaultMutableTreeNode(((MyFile)temp.getFatContent()).getMyfileAttributes().getFileName());
//                    me.setUserObject(temp);
//                    me = new DefaultMutableTreeNode(temp);
                }
                parent.add(me);                
            }            
        }
    }

    public void updateMyTree(MyFAT myfat,JTree temp){
    }
    
}
